CmdUtils.CreateCommand({
    name: "defineword",
    homepage: "http://www.erikvold.com/tools/ubiquity/dictionary/index.cfm",
    icon:"http://dictionary.reference.com/favicon.ico",
    description:"Search Dictionary.com for a word.",
    help:"Type or highlight a word to lookup the definition on dictionary.com",
    author: { name: "Erik Vold", email: "erikvvold@gmail.com"},
    contributors: ["Erik Vold"],
    license: "MPL",
    version: "0.1",
    takes: {"word": noun_arb_text},
    preview: function (pblock, directObject) {
        var word = jQuery.trim(directObject.text);
        
        var url = "http://dictionary.reference.com/browse/";

        // if the word is blank then display a default message
        if(word==""){
            pblock.innerHTML = 'Provide a word to search <a title="Dictionary.com" href="http://dictionary.reference.com/">dictionary.com</a>, or hit enter now to go there.';

            return;
        }

        // display the word which will be searched
        pblock.innerHTML = "Searching for " + word + ' on <a title="Dictionary.com" href="http://dictionary.reference.com/">dictionary.com...</a>';

        // search dictionary.com for the provided word
        jQuery.ajax({
            type:"GET",
            dataType:"text",
            url: url+word,
            error:function(){
                pblock.innerHTML = "There was an error.";
            },
            success:function(response){
                var responseStr = jQuery.trim(response+"");

                var doc = context.focusedWindow.document;

                var domDiv = doc.createElement( "div" );
                domDiv.innerHTML = response;

                var content = jQuery("div.lunatext", domDiv);

                if(content.length == 0){
                    pblock.innerHTML = "No direct matches.<br> <b>Hit enter for suggested alternative searches...</b>";

                    return;
                }

                pblock.innerHTML = "";
                for(var i =0; i< content.length; i++){
                    pblock.innerHTML += content.eq(i).html() + ",";
                }

                pblock.innerHTML += "<p><b>Hit enter for more information...</b></p>";
            }
        });

        return;
    },
    execute: function( directObject ) {
        var word = jQuery.trim(directObject.text);

        // if the string is blank then go directly to dictionary.com
        if (word.length <= 0) {
            Utils.openUrlInBrowser("http://dictionary.reference.com/");
            return;
        }

        // if the string is not blank then assume it is a word, and go to the dictionary.com page for the word
        Utils.openUrlInBrowser('http://dictionary.reference.com/browse/' + word);

        return;
    }
});


